﻿using Azure;
using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using SIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SService
{
    public class ProductTypeS: ProductTypeSI
    {
        private readonly WmssystemContext myDbContext;

        public ProductTypeS(WmssystemContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        //角色管理添加
        public Task<int> Add(PO_ProductType proAdd)
        {
            myDbContext.PO_ProductType.Add(proAdd);
            return myDbContext.SaveChangesAsync();
        }
        //角色分页模糊查询
        public Task<List<PO_ProductType>> FenYe(int pageSize, int currentPage, string Key = null, bool? Status = null, string Operator = null, DateTime? OperationTime = null)
        {
            var query = myDbContext.PO_ProductType.AsQueryable();

            // 根据条件进行查询
            if (!string.IsNullOrEmpty(Key))
            {
                query = query.Where(c => c.P_ProductTypeCode.Contains(Key)|| c.P_ProductTypeName.Contains(Key));
            }
            if (Status.HasValue)
            {
                query = query.Where(c => c.P_ProductTypeStatus == Status);
            }
            if (!string.IsNullOrEmpty(Operator))
            {
                query = query.Where(c => c.P_ProductTypeOperator.Contains(Operator));
            }
            if (OperationTime.HasValue)
            {
                query = query.Where(c => c.P_ProductTypeOperationTime >= OperationTime);
            }
            query = query.Where(c => c.P_PIsDelete == true);
            // 分页查询
            return query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        //修改
        public Task<int> Upd(PO_ProductType proUpd)
        {
            myDbContext.PO_ProductType.Update(proUpd);
            return myDbContext.SaveChangesAsync();
        }
        //逻辑删除
        public async Task<int> UpdIsDelete(int id, bool isDeleted)
        {
            var entity = await myDbContext.PO_ProductType.FindAsync(id);
            // 更新实体的删除状态
            entity.P_PIsDelete = isDeleted;
            // 标记实体为已修改状态
            myDbContext.Entry(entity).State = EntityState.Modified;
            // 保存更改到数据库
            return await myDbContext.SaveChangesAsync();
        }
        //级联
        public Task<List<PO_ProductType>> Erji(int pid) 
        {
            return myDbContext.PO_ProductType.Where(c => c.P_ProductTypePid == pid).ToListAsync();
        }
        public Task<List<PO_ProductType>> Yiji() 
        {
            return myDbContext.PO_ProductType.Where(c => c.P_ProductTypePid == 0).ToListAsync();
        }
    }
}
